<script setup lang="ts" name="SystemRole">
import { getPageList , deleteById } from '@/api/system/role';
import { ref, reactive , toRefs , onMounted, nextTick, defineAsyncComponent } from 'vue';
import { notify } from '@/utils/element';

const RoleEdit = defineAsyncComponent(() => import('@/views/system/role/components/role-edit.vue'));
const Permission = defineAsyncComponent(() => import('@/views/system/role/components/permission.vue'));

const editRef = ref();

const state = reactive({
  loading: false,
  page: {
    pageNum: 1,
    pageSize: 20,
    total: 0,
  } as PageType,
  query: {
    menuName: ''
  } as SysRoleQuery,
  tableList: [] as SysRoleType [],
});

const { loading, page, query, tableList } = { ...toRefs(state)};

onMounted(() => {
  queryData();
});

async function queryData() {
  try {
    state.loading = true;
    const { pageNum, pageSize } = state.page;
    const { data } = await getPageList(state.query, pageNum, pageSize);
    console.log('queryData',data);
    state.tableList = [];
    nextTick(() => {
      state.page.total = data.total;
      state.tableList = data;
    });
  } catch (error) {
  } finally {
    state.loading = false;
  }
}

function handleQuery() {
  //将页码设置为第一页
  state.page.pageNum = 1;
  queryData();
}

async function handleDelete(id: string) {
  //console.log('handleDelete', id);
  try {
    state.loading = true;
    await deleteById(id);
    notify('删除成功！',{type: 'success'});
    queryData();
  } catch (error) {
  } finally {
    state.loading = false;
  }
}

function handleAdd() {
  //console.log('handleAdd');
  editRef.value.open('add', '新增');
}

function handleEdit(row: SysRoleType) {
  //console.log('handleEdit', role);
  editRef.value.open('edit','修改',row);
}

const permissionRef = ref();
function handlePermission(role: SysRoleType) {
  //console.log('handlePermission',role);
  permissionRef.value.open(role);
}
</script>

<template>
  <div class="layout-padding">
    <el-form inline :model="query">
      <el-form-item label="角色名称">
        <el-input v-model="query.name" maxlength="30" placeholder="请输入角色名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery()" icon="ele-Search" type="primary">查询</el-button>
        <el-button @click="handleAdd()" icon="ele-Plus" type="success">新增角色</el-button>
      </el-form-item>
    </el-form>
    <el-table
      class="w100"
      ref="tableListRef"
      v-loading="loading"
      :data="tableList"
      border
      stripe
    >
      <el-table-column align="center" type="index" label="序号" width="60"/>
      <el-table-column align="center" prop="roleName" label="角色名称" />
      <el-table-column align="center" prop="roleKey" label="角色编码" />
      <el-table-column align="center" label="角色状态">
        <template #default="{row}">
          <el-tag :type="row.status == '0' ? 'success' : 'warning'">{{ row.status == '0' ? '启用' : '停用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="remark" label="备注" />
      <el-table-column fixed="right" align="center" label="操作" width="260">
        <template #default="{row}">
          <el-button @click.stop="handlePermission(row)" icon="ele-Check" type="primary" link>分配权限</el-button>
          <el-button @click.stop="handleEdit(row)" icon="ele-Edit" type="warning" link>修改</el-button>
          <el-popconfirm @confirm="handleDelete(row.id)" width="auto" :title="`确定永久删除【${row.roleName}】吗?`">
            <template #reference>
              <el-button icon="ele-Delete" type="danger" link>删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <m-page :page="page" @pageChange="queryData"/>
    <!--新增、修改组件-->
    <RoleEdit ref="editRef" @refresh="queryData"/>
    <!--分配权限-->
    <Permission ref="permissionRef"/>
  </div>
</template>

<style scoped>

</style>
